package cn.limeizi.shoppingcar.daos.impl;

import cn.limeizi.shoppingcar.MyTools;
import cn.limeizi.shoppingcar.beans.McBean;
import cn.limeizi.shoppingcar.beans.OrderItemBean;
import cn.limeizi.shoppingcar.daos.McDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李美姿
 * @Date: 2019/7/17 14:44
 * @Version 1.0
 * @描述：
 */
public class McDaoImpl implements McDao {

    private Connection connection;//数据库连接对象
    private PreparedStatement preparedStatement;//预编译对象
    private ResultSet resultSet;//查询接收对象

    /*
     * 初始化连接
     * */
    //新的独立连接
    public McDaoImpl() {
        connection = MyTools.getConn();
    }

    //来自外部连接
    public McDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public int addMc(McBean mcBean) {
        connection = MyTools.getConn();
        int out=0;
        try {
            String sql="insert into mc(sname,nprice,sdescription) values(?,?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,mcBean.getSname());
            preparedStatement.setDouble(2,mcBean.getNprice());
            preparedStatement.setString(3,mcBean.getSdescription());
            out=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }

        return out;
    }

    @Override
    public int deleteMc(int nid) {
        connection = MyTools.getConn();
        int out=0;
        try {
            String sql="delete * from mc where nid=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,nid);
            out=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public int updateMc(McBean mcBean) {
        connection = MyTools.getConn();
        int out=0;
        try {
            String sql="update mc set sname=?,nprice=?,sdescription=? where nid=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,mcBean.getSname());
            preparedStatement.setDouble(2,mcBean.getNprice());
            preparedStatement.setString(3,mcBean.getSdescription());
            preparedStatement.setInt(4,mcBean.getNid());
            out=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return out;
    }

    @Override
    public McBean getMc(int id) {
        connection = MyTools.getConn();
        try {
            //编写数据库语句
            String sql = "select * from mc where nid=?";
            //预编译数据库语句
            preparedStatement = connection.prepareStatement(sql);
            //设置预编译的值
            preparedStatement.setInt(1, id);
            //执行数据库语句
            resultSet = preparedStatement.executeQuery();
            //解析数据库返回数据集合
            while (resultSet.next()) {
                McBean mcBean = new McBean();
                mcBean.setNid(resultSet.getInt("nid"));
                mcBean.setSname(resultSet.getString("sname"));
                mcBean.setNprice(resultSet.getDouble("nprice"));
                mcBean.setSdescription(resultSet.getString("sdescription"));
                //返回用户对象
                return mcBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return null;
    }

    @Override
    public List<McBean> getAllMc() {

        connection=MyTools.getConn();
        List<McBean> mcBeans =new ArrayList<>();
        try {
            String sql="select * from mc";
            preparedStatement =connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                McBean mcBean =new McBean();
                mcBean.setNid(resultSet.getInt("nid"));
                mcBean.setSname(resultSet.getString("sname"));
                mcBean.setNprice(resultSet.getDouble("nprice"));
                mcBean.setSdescription(resultSet.getString("sdescription"));
                mcBeans.add(mcBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            MyTools.closeMysql(connection, preparedStatement, resultSet);//关闭数据库
        }
        return mcBeans;
    }


}
